package com.hsbc.dev.teamo4.sms.sm.dao.impl;

import com.google.common.collect.Lists;
import com.hsbc.dev.teamo4.sms.common.utils.data.DBUtil;
import com.hsbc.dev.teamo4.sms.sm.dao.IRoleMenuPermissionDao;
import com.hsbc.dev.teamo4.sms.sm.po.Permission;
import com.hsbc.dev.teamo4.sms.sm.po.RoleMenuPermission;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

/**
 * @author:Kevin
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.dao.impl
 * @Description:
 * @Date date: 2018/7/27
 */
public class RoleMenuPermissionDaoImpl implements IRoleMenuPermissionDao {

    @Override
    public RoleMenuPermission saveEntity(RoleMenuPermission roleMenuPermission) {
        String sql="insert into sm_role_menu_permission values(?,?,?,?,?,?,?,?,?)";
        return (RoleMenuPermission) DBUtil.saveEntity(sql,roleMenuPermission,null);
    }

    @Override
    public RoleMenuPermission updateEntity(RoleMenuPermission roleMenuPermission) {
        String sql="update  sm_role_menu_permission set  roleId=?,menuId=?,permissionId=?,updateId=?,updateTime=?,isEnable=? where id=?";
        return (RoleMenuPermission) DBUtil.updateEntity(sql,roleMenuPermission, Lists.newArrayList("id","createId","createTime"),roleMenuPermission.getId());
    }

    @Override
    public int updateByIsEnable(String id, Boolean bool) {
        String sql = "update sm_role_menu_permission set isEnable=? where id=?";
        return DBUtil.updateByIsEnable(sql,id,bool);
    }

    @Override
    public RoleMenuPermission queryById(String id) {
        String sql="select * from sm_role_menu_permission where id='"+id+"'";
        BeanHandler beanHandler=new BeanHandler<>(RoleMenuPermission.class);
        return (RoleMenuPermission) DBUtil.queryById(sql,beanHandler);
    }

    @Override
    public List<RoleMenuPermission> queryAllEntity() {
        String sql="select * from sm_role_menu_permission where isEnable='"+true+"'";
        BeanListHandler beanListHandler=new BeanListHandler<>(RoleMenuPermission.class);
        return (List<RoleMenuPermission>) DBUtil.queryAllEntity(sql,beanListHandler);
    }

    @Override
    public List<String> getPermissionIdListByRoleIdList(List<String> list) {
        List<String> permissionIdList=Lists.newArrayList();
        for (String roleId:list){
            String sql ="select permissionId from sm_role_menu_permission where roleId='"+roleId+"'";
            ArrayHandler arrayHandler=new ArrayHandler();
            List<String> resoult= (List<String>) DBUtil.queryString(sql,arrayHandler);
            if(resoult!=null){
                permissionIdList.addAll(resoult);
            }

        }
        return permissionIdList;
    }

}
